package com.yxhl.zoume.core.func.passenger.presenter;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.func.passenger.view.SelectPassengersView;
import com.yxhl.zoume.data.http.model.passenger.ZMPassenger;
import com.yxhl.zoume.data.http.rest.param.passenger.GetPassengersParam;
import com.yxhl.zoume.data.http.rest.param.passenger.PageOptions;
import com.yxhl.zoume.data.http.rest.response.passenger.GetPassengersResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.passenger.GetPassengersUseCase;
import com.yxhl.zoume.utils.CollectionUtils;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by Xboy on 16/4/22.
 */
public class SelectPsgersPresenter extends BasePresenter implements IPresenter {
    private SelectPassengersView selectPassengersView;
    private GetPassengersUseCase getPassengersUseCase;

    @Inject
    public SelectPsgersPresenter(Context context, GetPassengersUseCase getPassengersUseCase) {
        super(context);
        this.getPassengersUseCase = getPassengersUseCase;
    }


    public void getAddedPassengers(int page, int pageSize, final List<ZMPassenger> selectedPassengers) {
        selectPassengersView.waitingForQueryPassengers(true);

        PageOptions options = new PageOptions(page, pageSize);
        GetPassengersParam param = new GetPassengersParam(getMobile(), getToken(), options);
        getPassengersUseCase.execute(param, new ZMSubscriber<GetPassengersResponse>() {

            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                selectPassengersView.waitingForQueryPassengers(false);
            }

            @Override
            public void onNext(GetPassengersResponse getPassengersResponse) {
                super.onNext(getPassengersResponse);
                selectPassengersView.waitingForQueryPassengers(false);

                List<ZMPassenger> addedPassengers = getPassengersResponse.getAddedPassengers();
                // 标记已选乘客
                if (!CollectionUtils.isEmpty(selectedPassengers)) {
                    for (ZMPassenger sp : selectedPassengers) {
                        for (ZMPassenger ap : addedPassengers) {
                            if (sp.getPassengerId().equals(ap.getPassengerId())) {
                                ap.setSelected(true);
                            }
                        }
                    }
                }
                selectPassengersView.renderAddedPassengers(addedPassengers);
            }
        });
    }

    @Override
    public void attachView(BaseView view) {
        this.selectPassengersView = (SelectPassengersView) view;
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        getPassengersUseCase.unsubscribe();
    }
}
